package com.ztom.test

import com.ztom.beans.factory.DefaultListableBeanFactory
import com.ztom.beans.factory.support.extenssions.getBean
import com.ztom.beans.factory.support.reader.xml.XmlBeanDefinitionReader
import com.ztom.test.bean.Car
import com.ztom.test.bean.Person
import com.ztom.test.processor.CustomBeanFactoryPostProcessor
import com.ztom.test.processor.CustomBeanPostProcessor
import kotlin.test.Test
import kotlin.test.assertEquals

/**
 * @author ZhangTao
 */
class Test06PostProcessor {

    private val beanFactory: DefaultListableBeanFactory by lazy {
        val factory = DefaultListableBeanFactory()
        val xmlReader = XmlBeanDefinitionReader(factory)
        xmlReader.loadBeanDefinitions("classpath:spring-05-xml-define-bean.xml")
        factory
    }

    @Test
    fun testBeanFactoryPostProcessor() {
        val customBeanFactoryPostProcessor = CustomBeanFactoryPostProcessor("person", "age", 28)
        customBeanFactoryPostProcessor.postProcessBeanFactory(beanFactory)

        val person = beanFactory.getBean<Person>()
        println(person)
        assertEquals(person.age, 28)
    }

    @Test
    fun testBeanPostProcessor() {
        val customBeanPostProcessor = CustomBeanPostProcessor(Car::class.qualifiedName, "brand", "QQ")
        beanFactory.addBeanPostProcessor(customBeanPostProcessor)

        val car = beanFactory.getBean<Car>()
        println(car)
        assertEquals(car.brand, "QQ")
    }
}